package com.sdkx.app.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.adapter.MainMenuAdapter;
import com.sdkx.app.model.Sketch;

import org.litepal.LitePal;

import java.util.List;

public class DrawMapFragment extends BaseFragment {
    //航线管理二级目录
    //  private String[] CourseList = {"折线","圆弧","扇形", "圆形","矩形","多边形"};
    private ListView lvGroup;
    private SketchAdpater sketchAdpater;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_menu_mapdrawing, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lvGroup = (ListView) view.findViewById(R.id.sketchlist);
        sketchAdpater = new SketchAdpater();
        lvGroup.setAdapter(sketchAdpater);
        lvGroup.setOnItemClickListener(new MenuAdapterViewListener());
        //返回上一级点击事件
        Button tv_all_air_spaces = (Button) view.findViewById(R.id.tv_all_air_spaces2);

        //返回上一级按钮
        tv_all_air_spaces.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goBack();

            }
        });


    }

    //二级菜单点击事件
    private class MenuAdapterViewListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            goBack();
        }
    }

    private class SketchAdpater extends BaseAdapter {
        List<Sketch> list;

        public SketchAdpater() {
            list = LitePal.findAll(Sketch.class);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.grid_item, null);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.textView);
                convertView.setTag(holder);
                convertView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 60));
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(list.get(position).getName());

            return convertView;
        }

        class ViewHolder {
            TextView text;
        }

    }


}